import 'dart:convert';

import 'package:flutter_ahj_app/app/HttpRequest.dart';
import 'package:flutter_ahj_app/app/regist/bean/message_code.dart';
import 'package:flutter_ahj_app/base/basewidget/model/BaseModel.dart';
import 'package:rxdart/rxdart.dart';

class RegistModel extends BaseModel {
  BehaviorSubject<MessageCode> _dataObservable;

  BehaviorSubject<MessageCode> _datacodeObservable;

  BehaviorSubject<MessageCode> _dataregistObservable;

  Stream<MessageCode> get dataStream => _dataObservable.stream;

  Stream<MessageCode> get codeStream => _datacodeObservable.stream;

  Stream<MessageCode> get registCodeStream => _dataregistObservable.stream;

/*
name 用户名
Password 密码
* */
  getRegistData(String name, String password,String locationProvince,
      String locationProvinceCode,
      String locationCity,
      String locationCityCode,
      String locationDistrict,
      String locationDistrictCode) {
    _dataregistObservable = BehaviorSubject();
    HttpRequest.userRegist(name, password,
        locationProvince,
        locationProvinceCode,
        locationCity,
        locationCityCode,
        locationDistrict,
        locationDistrictCode).listen((request) {
      MessageCode userBeanEntity = MessageCode.fromJson(request);
      _dataregistObservable.add(userBeanEntity);
    }, onError: (e) {
      //请求异常等
      _dataregistObservable.add(null);
    });
  }

  getCheckData(String phone) {
    _dataObservable = BehaviorSubject();
    HttpRequest.getuserCheckPassword(phone).listen((request) {
      MessageCode userBeanEntity = MessageCode.fromJson(request);
      _dataObservable.add(userBeanEntity);
    }, onError: (e) {
      //请求异常等
      _dataObservable.add(null);
    });
  }

  userCheckCode(String phone, String code) {
    _datacodeObservable = BehaviorSubject();
    HttpRequest.userCheckCode(phone, code).listen((request) {
      MessageCode userBeanEntity = MessageCode.fromJson(request);
      _datacodeObservable.add(userBeanEntity);
    }, onError: (e) {
      //请求异常等
      _datacodeObservable.add(null);
    });
  }

  BehaviorSubject<Map> addressSubject = BehaviorSubject();

  Stream<Map> get addressStream => addressSubject.stream;

  getData(String name, String code) {
    addressSubject = BehaviorSubject();
    var data = {
      'name': name,
      'parentCode': code,
    };
    HttpRequest.getAddress(data).listen((request) {
      addressSubject.add(request);
    }, onError: (e) {
      //请求异常等
      addressSubject.add(null);
      errorData.add(e);
    });
  }

  Stream get errorStream => errorData;

  void dispose() {
    if (addressSubject != null) addressSubject.close();
    if (_dataObservable != null) {
      _dataObservable.close();
    }
    if (errorData != null) {
      errorData.close();
    }
    super.dispose();
  }
}
